import json


def jsonToRecords(white_locations, black_locations):
    """
    :param white_locations: 白棋坐标
    :param black_locations: 黑棋坐标
    把json函数转化为record数组
    """
    # 初始化棋盘信息
    records = [[None for i in range(15)] for j in range(15)]
    for i in range(0, 15):
        for j in range(0, 15):
            records[i][j] = Record(-1)
    # 把json里的棋盘写入数组
    for white_item in white_locations:
        x = white_item['x']
        y = white_item['y']
        records[x][y] = Record(1)
    for black_item in black_locations:
        x = black_item['x']
        y = black_item['y']
        records[x][y] = Record(0)
    return records

def print_records(records):
    """
    :param records: 棋盘数组
    打印棋盘信息
    """
    for i in range(0, 15):
        for j in range(0, 15):
            if records[i][j]:
                print("%2d" % records[i][j].color, end="")
            else:
                print(records[i][j], end="")
        print("")

def analysis_game_data(_lcattrs):
    """
    :param _lcattrs: json数组
    分析五子棋数棋盘逻辑
    """
    # 读入变量
    if 'winner_clientids' in _lcattrs:
        winner_clientids = _lcattrs['winner_clientids']
    if 'game_data' not in _lcattrs:
        print(_lcattrs)
        return
    if 'game_state' not in _lcattrs:
        return
    if  _lcattrs['game_state'] != 3:
        return
    game_data = _lcattrs['game_data']
    current_position = game_data['current_position']
    current_color = game_data['current_color']
    records = jsonToRecords(game_data['white_locations'], game_data['black_locations'])
    print_records(records)
    # 创建五子棋类
    gomoku = Gomoku(records)
    color = -1
    # 检查胜负
    if(gomoku.check(current_position['x'],current_position['y'],color)):
        _lcattrs['game_state'] = 4

class Record:
    color = -1
    def __init__(self, color):
        self.color = color
    """
    color: 0 white
    color: 1 black
    color: -1 empty
    """


class Gomoku:
    def __init__(self, records):
        """
        :param record: 棋盘数组
        初始化棋盘信息
        """
        self.records = records
        self.height = 15
        self.width = 15

    def has_record(self, x, y):
        """
        :param x: 棋子横坐标
        :param y: 棋子中坐标
        :return: 返回某处是否已经落子,通过判断棋盘位置是否是-1 如果不是就是已经落子的了
        """
        if 0 <= x < 15 and 0 <= y < 15:
            if self.records[x][y]:
                return not self.records[x][y] == -1
        return False

    def check(self, x, y, color):
        """
        :param x: 最后一颗子x坐标
        :param y: 最后一颗子y坐标
        :return:
        检测是否获胜
        """
        # 记录起始点
        root = [x, y]
        root_color = self.records[x][y].color
        # 记录错误
        if root_color == -1:
            return False
        # 开始从横竖斜三个方向查找最大连续长度
        for pos in [[[-1, 0],[1, 0]], [[-1, -1],[1, 1]], [[0, 1],[0, -1]], [[-1, 1], [1, -1]]]:
            lengh = 0
            # 先从一个方向搜索
            while self.has_record(x, y) and self.records[x][y].color == root_color:
                x += pos[0][0]
                y += pos[0][1]
                lengh += 1
            x, y = root
            # 回退到起点再从另一个方向搜索
            while self.has_record(x, y) and self.records[x][y].color == root_color:
                x += pos[1][0]
                y += pos[1][1]
                lengh += 1
            x, y = root
            # 判断是否五颗子(计数应该是6次,起点会重复计数)
            if lengh >= 6:
                print(lengh, pos, root, root_color)
                return True
        return False
            
        
            
        


if __name__ == '__main__':
    # 测试代码
    records = [[Record(-1) for i in range(15)] for j in range(15)]
    
    records[1][0] = Record(0)
    records[1][1] = Record(0)
    records[1][2] = Record(0)
    records[1][3] = Record(0)
    records[1][4] = Record(0)
    records[2][0] = Record(1)
    records[2][1] = Record(1)
    records[2][2] = Record(1)
    records[2][3] = Record(1)
    gomoku = Gomoku(records)
    print_records(records)
    print(gomoku.check(1,3,1)) #竖行
    print(gomoku.check(2,4,1)) #竖行
    records = [[Record(-1) for i in range(15)] for j in range(15)]

    records[0][0] = Record(0)
    records[1][0] = Record(0)
    records[2][0] = Record(0)
    records[3][0] = Record(0)
    records[4][0] = Record(0)
    records[0][1] = Record(1)
    records[1][1] = Record(1)
    records[2][1] = Record(1)
    records[3][1] = Record(1)
    gomoku = Gomoku(records)
    print(gomoku.check(4,1,1)) #横行
    print(gomoku.check(4,2,1)) #横行
    records = [[Record(-1) for i in range(15)] for j in range(15)]

    records[0][0] = Record(0)
    records[1][1] = Record(0)
    records[2][2] = Record(0)
    records[3][3] = Record(0)
    records[4][4] = Record(0)
    records[1][2] = Record(1)
    records[2][3] = Record(1)
    records[3][4] = Record(1)
    records[4][5] = Record(1)
    gomoku = Gomoku(records)
    print(gomoku.check(5,6,1)) #斜行
    print(gomoku.check(5,7,1)) #斜行
    records = [[Record(-1) for i in range(15)] for j in range(15)]

    records[0][5] = Record(0)
    records[1][4] = Record(0)
    records[2][3] = Record(0)
    records[3][2] = Record(0)
    # records[4][1] = Record(0)
    gomoku = Gomoku(records)
    print(gomoku.check(4,1,0)) #斜行
    print(gomoku.check(4,1,1)) #斜行
    
